from django.shortcuts import render
from django.http import HttpResponse

from django.http import JsonResponse
from django.shortcuts import render, render_to_response
from blog import models


# Create your views here.


def login(request):
    jos = {"h": "h"}

    if request.method == 'GET':
        ID = request.GET.get("name", default='110')
    c = models.Category()
    c.index = 1
    c.name = ID
    c.save()
    print(ID)
    return JsonResponse(jos)


def session_test(request):  # 查询并且设置session

    obj = models.Category.objects.filter(name="李智超")[0]  # 必须设置长度否则报错

    if obj:
        request.session['name'] = obj.name
        print(request.session['name'])

        response =HttpResponse('ok')
        response.set_cookie('hello', "lizhichao", expires=60 * 60 * 24 * 7)  # 不能有中文，建议js 设置cookie。
    jos = {"mes": "success"}
    return response


def cookie_test(request):
    n = request.COOKIES.get('user')
    print(n)
    jos = {"mes": "success"}
    return JsonResponse(jos)
